home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / polardrw / data.z / DrawDlgDlg.cpp < prev    next >
C/C++ Source or Header  |  1999-05-24  |  7KB  |  252 lines

  1. // DrawDlgDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "DrawDlg.h"
  6. #include "DrawDlgDlg.h"
  7. #include "math.h"
  8. #include "pdrawconst.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CDrawDlgDlg dialog
  18.  
  19. CDrawDlgDlg::CDrawDlgDlg(CWnd* pParent /*=NULL*/)
  20.     : CDialog(CDrawDlgDlg::IDD, pParent)
  21. {
  22.     //{{AFX_DATA_INIT(CDrawDlgDlg)
  23.     m_uAngle = 0;
  24.     m_fTicking = FALSE;
  25.     m_fBorder = FALSE;
  26.     //}}AFX_DATA_INIT
  27.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  28.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  29.    m_lHHID = -1;
  30.    m_lMMID = -1;
  31.    m_lSSID = -1;
  32.     m_fBorder = FALSE;
  33.     m_nTimer = 0;
  34. }
  35.  
  36. void CDrawDlgDlg::DoDataExchange(CDataExchange* pDX)
  37. {
  38.     CDialog::DoDataExchange(pDX);
  39.     //{{AFX_DATA_MAP(CDrawDlgDlg)
  40.     DDX_Control(pDX, IDC_POLARDRAWCTRL1, m_Canvas);
  41.     DDX_Check(pDX, IDC_CHECK1, m_fTicking);
  42.     DDX_Check(pDX, IDC_BORDER, m_fBorder);
  43.     //}}AFX_DATA_MAP
  44. }
  45.  
  46. BEGIN_MESSAGE_MAP(CDrawDlgDlg, CDialog)
  47.     //{{AFX_MSG_MAP(CDrawDlgDlg)
  48.     ON_WM_PAINT()
  49.     ON_WM_QUERYDRAGICON()
  50.     ON_WM_TIMER()
  51.     ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
  52.     ON_BN_CLICKED(IDC_BORDER, OnBorder)
  53.     ON_WM_DESTROY()
  54.     //}}AFX_MSG_MAP
  55. END_MESSAGE_MAP()
  56.  
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CDrawDlgDlg message handlers
  59. CPoint ptCenter(0, 0);
  60.  
  61.  
  62. BOOL CDrawDlgDlg::OnInitDialog()
  63. {
  64.     CDialog::OnInitDialog();
  65.  
  66.     // Set the icon for this dialog.  The framework does this automatically
  67.     //  when the application's main window is not a dialog
  68.     SetIcon(m_hIcon, TRUE);            // Set big icon
  69.     SetIcon(m_hIcon, FALSE);        // Set small icon
  70.     
  71.     // TODO: Add extra initialization here
  72.    m_Canvas.SetEnableRendering(FALSE);
  73.    m_Canvas.SetRecordUndo(FALSE);
  74.  
  75.    CRect rc;
  76.    GetDlgItem(IDC_POLARDRAWCTRL1)->GetClientRect(&rc);
  77.    m_Canvas.SetPageWidth(rc.Width()); //pixels
  78.    m_Canvas.SetPageHeight(rc.Height()); //pixels
  79.    m_Canvas.SetPageOrigin(0, 0); //pixels
  80.    m_Canvas.SetViewportOrigin(0, 0); //pixels
  81.  
  82.     long l = 0;
  83.    long lRad = 90;
  84.    long lX, lY;
  85.    long lID;
  86.  
  87.    while(l < 12)
  88.    {
  89.       float lAngle = (float)(l*30 / (360 / (2*3.1416)));
  90.       lX = ptCenter.x - (long)(sin(lAngle) * lRad);
  91.       lY = ptCenter.y - (long)(cos(lAngle) * lRad);
  92.       if((l*30) % 90 == 0)
  93.          lID = m_Canvas.AddShape(polIsoscelesTriangle, lX - 10, lY -10, lX +10, lY+10);
  94.       else
  95.          lID = m_Canvas.AddShape(polIsoscelesTriangle, lX - 8, lY -8, lX +8, lY+8);
  96.      
  97.         m_Canvas.SetShapeRotation(lID, l*30);
  98.  
  99.       m_Canvas.SetShapeFillColor(lID, RGB(0,0,255));
  100.  
  101.       l++;
  102.    }
  103.  
  104.    lX = ptCenter.x;
  105.    lY = ptCenter.y;
  106.  
  107.     // ID of hour handle
  108.    m_lHHID = m_Canvas.AddShape(polLeftArrow, lX - 40, lY -30, lX +40, lY+30);
  109.    m_Canvas.SetShapeRotation(m_lHHID, -90);
  110.     m_Canvas.MoveShape(m_lHHID, 0, -30);
  111.     // ID of minute handle
  112.    m_lMMID = m_Canvas.AddShape(polIsoscelesTriangle, lX - 10, lY -100, lX +10, lY+10);
  113.    m_Canvas.SetShapeFillColor(m_lMMID, RGB(255,0,0));
  114.     // ID of seconds handle
  115.    m_lSSID = m_Canvas.AddShape(polRectangle, lX - 2, lY -95, lX +2, lY+10);
  116.  
  117.  
  118.    m_Canvas.SetShapeFillColor(m_lSSID, RGB(0,0,255));
  119.    m_Canvas.SelectAll();
  120.    m_Canvas.FitToSelection();
  121.    m_Canvas.ClearSelection();
  122.  
  123.  
  124.    m_nTimer = SetTimer(1, 1000, NULL);
  125.     // enable rendering from here
  126.    m_Canvas.SetEnableRendering(TRUE);
  127.        
  128.     return TRUE;  // return TRUE  unless you set the focus to a control
  129. }
  130.  
  131. // If you add a minimize button to your dialog, you will need the code below
  132. //  to draw the icon.  For MFC applications using the document/view model,
  133. //  this is automatically done for you by the framework.
  134.  
  135. void CDrawDlgDlg::OnPaint() 
  136. {
  137.     if (IsIconic())
  138.     {
  139.         CPaintDC dc(this); // device context for painting
  140.  
  141.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  142.  
  143.         // Center icon in client rectangle
  144.         int cxIcon = GetSystemMetrics(SM_CXICON);
  145.         int cyIcon = GetSystemMetrics(SM_CYICON);
  146.         CRect rect;
  147.         GetClientRect(&rect);
  148.         int x = (rect.Width() - cxIcon + 1) / 2;
  149.         int y = (rect.Height() - cyIcon + 1) / 2;
  150.  
  151.         // Draw the icon
  152.         dc.DrawIcon(x, y, m_hIcon);
  153.     }
  154.     else
  155.     {
  156.         CDialog::OnPaint();
  157.     }
  158. }
  159.  
  160. // The system calls this to obtain the cursor to display while the user drags
  161. //  the minimized window.
  162. HCURSOR CDrawDlgDlg::OnQueryDragIcon()
  163. {
  164.     return (HCURSOR) m_hIcon;
  165. }
  166.  
  167. void CDrawDlgDlg::OnTimer(UINT nIDEvent) 
  168. {
  169.     // TODO: Add your message handler code here and/or call default
  170.     if(nIDEvent == m_nTimer)
  171.     {
  172.         CTime time = CTime::GetCurrentTime();
  173.         long lHH = time.GetHour();
  174.         long lMM = time.GetMinute();
  175.         long lSS = time.GetSecond();
  176.         m_Canvas.SetEnableRendering(FALSE);
  177.  
  178.         long lX = ptCenter.x;
  179.         long lY = ptCenter.y;
  180.  
  181.         // set shapes to initial position
  182.         m_Canvas.SetShapePos(m_lHHID, lX - 40, lY -30, lX +40, lY+30);
  183.         m_Canvas.SetShapeRotation(m_lHHID, -90);
  184.         m_Canvas.MoveShape(m_lHHID, 0, -30);
  185.         m_Canvas.SetShapePos(m_lMMID, lX - 10, lY -100, lX +10, lY+10);
  186.         m_Canvas.SetShapeRotation(m_lMMID, 0);
  187.         m_Canvas.SetShapeFillColor(m_lMMID, RGB(255,0,0));
  188.         m_Canvas.SetShapePos(m_lSSID, lX - 2, lY -95, lX +2, lY+10);
  189.         m_Canvas.SetShapeRotation(m_lSSID, 0);
  190.  
  191.         // rotate them around center according to the current time
  192.         m_Canvas.RotateShapeAroundPoint(m_lHHID, ptCenter.x, ptCenter.y, -(long)((float(lHH + lMM/60.0)) * 30));
  193.         m_Canvas.RotateShapeAroundPoint(m_lMMID, ptCenter.x, ptCenter.y, -lMM * 6 );
  194.         m_Canvas.RotateShapeAroundPoint(m_lSSID, ptCenter.x, ptCenter.y, -lSS * 6 );
  195.             
  196.         long crVal = (long)(255 * ((lSS % 60) / 60.0));
  197.         m_Canvas.SetEnableRendering(TRUE);
  198.         m_Canvas.SetShapeFillColor(m_lHHID, RGB(crVal, 255, 0));
  199.         if(m_fTicking)
  200.             MessageBeep(1);
  201.     }
  202.     CDialog::OnTimer(nIDEvent);
  203. }
  204.  
  205. void CDrawDlgDlg::OnOK() 
  206. {
  207.     CDialog::OnOK();
  208. }
  209.  
  210. void CDrawDlgDlg::OnCheck1() 
  211. {
  212.     UpdateData(TRUE);
  213. }
  214.  
  215. void CDrawDlgDlg::OnBorder() 
  216. {
  217.     UpdateData(TRUE);
  218.  
  219.     // change the style of border:
  220.     if(m_fBorder)
  221.     {
  222.         m_Canvas.SetAppearance(1);
  223.         m_Canvas.SetBorderStyle(1);
  224.     }
  225.     else
  226.     {
  227.         m_Canvas.SetAppearance(0);
  228.         m_Canvas.SetBorderStyle(0);
  229.     }
  230.  
  231.     // stretch the canvas to fit the window
  232.     m_Canvas.SetEnableRendering(FALSE);
  233.    m_Canvas.SelectAll();
  234.    m_Canvas.FitToSelection();
  235.     m_Canvas.SetEnableRendering(TRUE);
  236.     // this call will re-render canvas
  237.    m_Canvas.ClearSelection();
  238.  
  239.  
  240. }             
  241.  
  242. void CDrawDlgDlg::OnDestroy() 
  243. {
  244.     CDialog::OnDestroy();
  245.     
  246.     // Now, kill the timer that we created
  247.     if(m_nTimer != 0)
  248.     {
  249.         KillTimer(m_nTimer);
  250.     }
  251. }
  252.